define(["angularAMD"], function (angularAMD) {
    angularAMD.service('SaveTemplateService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService) {

            var service = this;

            // model_no : 1(正向追溯)、2(生產追溯)
            service.init = function(option, call_back){
                var user_template = [
                    {
                        model_name: '',
                        mode_sheets: []
                    }
                ];
                user_template[0].mode_sheets = option.panel_list.filter((sheet) => sheet.visible).map((sheet) => ({
                    mode_sheet: sheet.panel_code,
                    seq: sheet.seq
                }));
                openDialog(option.model_no, user_template, call_back);
            };

            function openDialog(model_no, user_template, call_back){
                $mdDialog.dialog(
                    'module/moduleQT/program/service/SaveTemplateService.html',
                    function (dialog) {
                        return {
                            css_url:'module/moduleQT/program/service/SaveTemplateService.css',
                            model_no: model_no,
                            user_template: user_template,
                            back:function(){
                                dialog.hide();
                            },
                            confirm:function(){
                                if(!dialog.user_template[0].model_name) return

                                dialog.hide();
                                var content = {
                                    // model_no: model_no,
                                    request_type: 1,
                                    user_template: dialog.user_template
                                };
                                userTemplateProcess(content, function() {
                                    // if(data.Code == '0'){
                                    //     $rootScope.showToast($filter('translate')('QT_standard.common.saveModule.msg.save_success'));
                                    //     dialog.hide();
                                    // } else {
                                    //     $rootScope.showAlert($filter('translate')('QT_standard.common.saveModule.msg.save_fail'));
                                    // };
                                    call_back(dialog.model_no)
                                });
                            }
                        };
                    }
                );
            };

            // 模板更新服務: 新增，檢查，調整排序
            function userTemplateProcess(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.user_template_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };
        }
    ])
});
